/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MediumManagerFrame.java
 *
 * Created on 5.5.2011, 14:04:17
 */

package GUI;


import Model.Config;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.apache.commons.dbcp.BasicDataSource;
import javax.sql.DataSource;
import java.sql.SQLException;
import Model.*;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.concurrent.ExecutionException;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/**
 *
 * @author pyty
 */
public class MainFrame extends javax.swing.JFrame {

    MediumTableModel mediumModel;
    ClientTableModel clientModel;
    Locale locale = Config.LOCALE;
    ResourceBundle texts = ResourceBundle.getBundle(Config.TEXTS_FILE, locale);
   
    private LoadMediumsSwingWorker loadMediumsSwingWorker;
    private AddMediumSwingWorker addMediumSwingWorker;
    private DeleteMediumSwingWorker deleteMediumSwingWorker;

    private LoadClientsSwingWorker loadClientsSwingWorker;
    private AddClientSwingWorker addClientSwingWorker;
    private DeleteClientSwingWorker deleteClientSwingWorker;
    
    MediumManagerImpl mediumManager = new MediumManagerImpl();
    ClientManagerImpl clientManager = new ClientManagerImpl();
 
    private class LoadMediumsSwingWorker extends SwingWorker<List<Medium>,Void> {

        private String searchString = null;
        
        @Override    
        protected List<Medium> doInBackground() throws Exception {
            
            try {
                DataSource ds = prepareDataSource();
                mediumManager.setDataSource(ds);
                List<Medium> mediums;
                if(searchString == null) {
                    mediums = mediumManager.findAllMediums();
                } else {
                    mediums = mediumManager.searchMediumsByString(searchString);
                }
                return mediums;
//                for(Medium m : mediums) {
//                    mediumModel.addMedium(m);
//                }
            } catch (SQLException ex) {
                throw new RuntimeException("DB Error",ex);
            }
        }
        
        @Override    
        protected void done() {
            try {
                //jTextArea.append("Result is: " + get() + "\n");
                List<Medium> mediums = get();
                mediumModel.removeAll();
            
                for(Medium m : mediums) {
                    mediumModel.addMedium(m);
                }
            } catch (ExecutionException ex) {
                JOptionPane.showMessageDialog(null, "Error: " + ex);
            } catch (InterruptedException ex) {
                // K tomuto by v tomto případě nemělo nikdy dojít (viz níže)
                throw new RuntimeException("Operation interrupted (this should never happen)",ex);
            }
        }  
        
        public void setSearchString(String s) {
            this.searchString = s;
        }
    }
    
    private class AddMediumSwingWorker extends SwingWorker<Medium,Void> {
        private Medium medium = null;
        
        public void setMedium(Medium medium) {
            this.medium = medium;
        }

        @Override    
        protected Medium doInBackground() throws Exception {
            try {
                DataSource ds = prepareDataSource();
                mediumManager.setDataSource(ds);
                mediumManager.createMedium(medium);
                return medium;
            } catch (SQLException ex) {
                throw new RuntimeException("DB Error",ex);
            }
        }
        
        @Override    
        protected void done() {
            try {
                //jTextArea.append("Result is: " + get() + "\n");
                Medium medium = get();
                mediumModel.addMedium(medium);
            } catch (ExecutionException ex) {
                JOptionPane.showMessageDialog(null, "Error: " + ex);
            } catch (InterruptedException ex) {
                // K tomuto by v tomto případě nemělo nikdy dojít (viz níže)
                throw new RuntimeException("Operation interrupted (this should never happen)",ex);
            }
        }        
    }
    
    private class DeleteMediumSwingWorker extends SwingWorker<Medium,Void> {
        private Medium medium = null;
        
        public void setMedium(Medium medium) {
            this.medium = medium;
        }
        
        @Override    
        protected Medium doInBackground() throws Exception {
            
            try {
                DataSource ds = prepareDataSource();
                mediumManager.setDataSource(ds);
                mediumManager.deleteMedium(medium);
                return medium;
            } catch (SQLException ex) {
                throw new RuntimeException("DB Error",ex);
            }
        }
        
        @Override    
        protected void done() {
            try {
                Medium medium = get();
                mediumModel.deleteValue(medium);
                
            } catch (ExecutionException ex) {
                JOptionPane.showMessageDialog(null, "Error: " + ex);
            } catch (InterruptedException ex) {
                // K tomuto by v tomto případě nemělo nikdy dojít (viz níže)
                throw new RuntimeException("Operation interrupted (this should never happen)",ex);
            }
        } 
    }

    private class LoadClientsSwingWorker extends SwingWorker<List<Client>,Void> {

        private String searchString = null;

        @Override
        protected List<Client> doInBackground() throws Exception {

            try {
                DataSource ds = prepareDataSource();
                mediumManager.setDataSource(ds);
                List<Client> clients;
                if(searchString == null) {
                    clients = clientManager.findAllClients();
                } else {
                    clients = clientManager.searchClientsByString(searchString);
                }
                return clients;
//                for(Client client : clients) {
//                    mediumModel.addClient(m);
//                }
            } catch (SQLException ex) {
                throw new RuntimeException("DB Error",ex);
            }
        }

        @Override
        protected void done() {
            try {
                //jTextArea.append("Result is: " + get() + "\n");
                List<Client> clients = get();
                clientModel.removeAll();

                for(Client c : clients) {
                    clientModel.addClient(c);
                }
            } catch (ExecutionException ex) {
                JOptionPane.showMessageDialog(null, "Error: " + ex);
            } catch (InterruptedException ex) {
                // K tomuto by v tomto případě nemělo nikdy dojít (viz níže)
                throw new RuntimeException("Operation interrupted (this should never happen)",ex);
            }
        }

        public void setSearchString(String s) {
            this.searchString = s;
        }
    }

    private class AddClientSwingWorker extends SwingWorker<Client,Void> {
        private Client client = null;

        public void setClient(Client client) {
            this.client = client;
        }

        @Override
        protected Client doInBackground() throws Exception {
            try {
                DataSource ds = prepareDataSource();
                clientManager.setDataSource(ds);
                clientManager.createClient(client);
                return client;
            } catch (SQLException ex) {
                throw new RuntimeException("DB Error",ex);
            }
        }

        @Override
        protected void done() {
            try {
                //jTextArea.append("Result is: " + get() + "\n");
                Client client = get();
                clientModel.addClient(client);
            } catch (ExecutionException ex) {
                JOptionPane.showMessageDialog(null, "Error: " + ex);
            } catch (InterruptedException ex) {
                // K tomuto by v tomto případě nemělo nikdy dojít (viz níže)
                throw new RuntimeException("Operation interrupted (this should never happen)",ex);
            }
        }
    }

    private class DeleteClientSwingWorker extends SwingWorker<Client,Void> {
        private Client client = null;

        public void setClient(Client client) {
            this.client = client;
        }

        @Override
        protected Client doInBackground() throws Exception {

            try {
                DataSource ds = prepareDataSource();
                clientManager.setDataSource(ds);
                clientManager.deleteClient(client);
                return client;
            } catch (SQLException ex) {
                throw new RuntimeException("DB Error",ex);
            }
        }

        @Override
        protected void done() {
            try {
                Client client = get();
                clientModel.deleteValue(client);

            } catch (ExecutionException ex) {
                JOptionPane.showMessageDialog(null, "Error: " + ex);
            } catch (InterruptedException ex) {
                // K tomuto by v tomto případě nemělo nikdy dojít (viz níže)
                throw new RuntimeException("Operation interrupted (this should never happen)",ex);
            }
        }
    }

    
    public void addMedium(Medium medium) {
        addMediumSwingWorker = new AddMediumSwingWorker();
        addMediumSwingWorker.setMedium(medium);
        addMediumSwingWorker.execute();
    }
    public void addClient(Client client) {
        addClientSwingWorker = new AddClientSwingWorker();
        addClientSwingWorker.setClient(client);
        addClientSwingWorker.execute();
    }
    
    /** Creates new form MediumManagerFrame */
    public MainFrame() {
        initComponents();
        
        mediumModel = (MediumTableModel) jTableMediums.getModel();
        clientModel = (ClientTableModel) jTableClients.getModel();
        /*try {
            model.getTableFromDB();
        } catch (SQLException ex) {
            throw new RuntimeException("DB Error",ex);
        }*/
      
        //try {
            //DataSource ds = prepareDataSource();
            loadMediumsSwingWorker = new LoadMediumsSwingWorker();
            loadMediumsSwingWorker.execute();

            loadClientsSwingWorker = new LoadClientsSwingWorker();
            loadClientsSwingWorker.execute();
            
            /*ClientManagerImpl clientManager = new ClientManagerImpl();
            clientManager.setDataSource(ds);
            List<Client> clients = clientManager.findAllClients();

            for(Client c : clients) {
                clientModel.addClient(c);
            }*/
            
        /*} catch (SQLException ex) {
            throw new RuntimeException("DB Error",ex);
        }*/

    }
    
    private String getBoundle(String key) {
        return texts.getString(key);
    }
    
    private static DataSource prepareDataSource() throws SQLException {
        DBConfig dbConfig = new DBConfig();
        
        BasicDataSource ds = new BasicDataSource();
        ds.setUrl(dbConfig.getDbUrl());
        ds.setUsername(dbConfig.getDbUser());
        ds.setPassword(dbConfig.getDbPass());
        
        ds.setDriverClassName(dbConfig.getDbDriver());

        return ds;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenuMediumList = new javax.swing.JPopupMenu();
        jMenuItemAddMedium = new javax.swing.JMenuItem();
        jMenuItemEditMedium = new javax.swing.JMenuItem();
        jMenuItemDeleteMedium = new javax.swing.JMenuItem();
        jMenuItemBorrowMedium = new javax.swing.JMenuItem();
        jTabbedPaneMain = new javax.swing.JTabbedPane();
        jPanelMediumManager = new javax.swing.JPanel();
        jScrollPaneMediumsTableCover = new javax.swing.JScrollPane();
        jTableMediums = new javax.swing.JTable();
        jButtonAddMedium = new javax.swing.JButton();
        jButtonEditMedium = new javax.swing.JButton();
        jButtonDeleteMedium = new javax.swing.JButton();
        jButtonBorrowMedium = new javax.swing.JButton();
        jLabelSearch = new javax.swing.JLabel();
        jTextFieldSearchMedium = new javax.swing.JTextField();
        jButtonSearchMedium = new javax.swing.JButton();
        jPanelClientManager = new javax.swing.JPanel();
        addClient = new javax.swing.JButton();
        editClient = new javax.swing.JButton();
        deleteClient = new javax.swing.JButton();
        showAllBorrows = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableClients = new javax.swing.JTable();

        jMenuItemAddMedium.setText("Add Medium");
        jMenuItemAddMedium.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemAddMediumActionPerformed(evt);
            }
        });
        jPopupMenuMediumList.add(jMenuItemAddMedium);

        jMenuItemEditMedium.setText("Edit Medium");
        jMenuItemEditMedium.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemEditMediumActionPerformed(evt);
            }
        });
        jPopupMenuMediumList.add(jMenuItemEditMedium);

        jMenuItemDeleteMedium.setText("Delete Medium");
        jMenuItemDeleteMedium.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemDeleteMediumActionPerformed(evt);
            }
        });
        jPopupMenuMediumList.add(jMenuItemDeleteMedium);

        jMenuItemBorrowMedium.setText("Borrow Medium");
        jMenuItemBorrowMedium.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemBorrowMediumActionPerformed(evt);
            }
        });
        jPopupMenuMediumList.add(jMenuItemBorrowMedium);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle(getBoundle("mediumEvidence"));
        setLocationByPlatform(true);
        setMinimumSize(new java.awt.Dimension(700, 590));

        jTableMediums.setModel(new MediumTableModel());
        jTableMediums.setComponentPopupMenu(jPopupMenuMediumList);
        jTableMediums.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTableMediums.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jTableMediumsMouseReleased(evt);
            }
        });
        jScrollPaneMediumsTableCover.setViewportView(jTableMediums);

        jButtonAddMedium.setText(getBoundle("addMedium"));
        jButtonAddMedium.setFocusable(false);
        jButtonAddMedium.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonAddMedium.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonAddMedium.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddMediumActionPerformed(evt);
            }
        });

        jButtonEditMedium.setText(getBoundle("editMedium"));
        jButtonEditMedium.setFocusable(false);
        jButtonEditMedium.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonEditMedium.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonEditMedium.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditMediumActionPerformed(evt);
            }
        });

        jButtonDeleteMedium.setText(getBoundle("deleteMedium"));
        jButtonDeleteMedium.setFocusable(false);
        jButtonDeleteMedium.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonDeleteMedium.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonDeleteMedium.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteMediumActionPerformed(evt);
            }
        });

        jButtonBorrowMedium.setText(getBoundle("borrowMedium"));
        jButtonBorrowMedium.setFocusable(false);
        jButtonBorrowMedium.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonBorrowMedium.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonBorrowMedium.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBorrowMediumActionPerformed(evt);
            }
        });

        jLabelSearch.setText(getBoundle("search") + ":");

        jButtonSearchMedium.setText(getBoundle("search"));
        jButtonSearchMedium.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchMediumActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelMediumManagerLayout = new javax.swing.GroupLayout(jPanelMediumManager);
        jPanelMediumManager.setLayout(jPanelMediumManagerLayout);
        jPanelMediumManagerLayout.setHorizontalGroup(
            jPanelMediumManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelMediumManagerLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButtonAddMedium)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonEditMedium)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonDeleteMedium)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonBorrowMedium)
                .addGap(18, 18, 18)
                .addComponent(jLabelSearch)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldSearchMedium, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonSearchMedium)
                .addContainerGap(64, Short.MAX_VALUE))
            .addComponent(jScrollPaneMediumsTableCover, javax.swing.GroupLayout.DEFAULT_SIZE, 806, Short.MAX_VALUE)
        );
        jPanelMediumManagerLayout.setVerticalGroup(
            jPanelMediumManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelMediumManagerLayout.createSequentialGroup()
                .addGroup(jPanelMediumManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanelMediumManagerLayout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(jButtonAddMedium, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanelMediumManagerLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanelMediumManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelMediumManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabelSearch, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jTextFieldSearchMedium, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButtonSearchMedium))
                            .addComponent(jButtonDeleteMedium, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButtonEditMedium, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButtonBorrowMedium, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPaneMediumsTableCover, javax.swing.GroupLayout.DEFAULT_SIZE, 491, Short.MAX_VALUE))
        );

        jTabbedPaneMain.addTab(getBoundle("mediumManager"), jPanelMediumManager);

        addClient.setText("Add Client");
        addClient.setFocusable(false);
        addClient.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        addClient.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        addClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addClientActionPerformed(evt);
            }
        });

        editClient.setText("Edit Client");
        editClient.setFocusable(false);
        editClient.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        editClient.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        editClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editClientActionPerformed(evt);
            }
        });

        deleteClient.setText("Delete Client");
        deleteClient.setFocusable(false);
        deleteClient.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        deleteClient.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        showAllBorrows.setText("Show all borrows");
        showAllBorrows.setFocusable(false);
        showAllBorrows.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        showAllBorrows.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        showAllBorrows.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showAllBorrowsActionPerformed(evt);
            }
        });

        jTableClients.setModel(new ClientTableModel());
        jScrollPane2.setViewportView(jTableClients);

        javax.swing.GroupLayout jPanelClientManagerLayout = new javax.swing.GroupLayout(jPanelClientManager);
        jPanelClientManager.setLayout(jPanelClientManagerLayout);
        jPanelClientManagerLayout.setHorizontalGroup(
            jPanelClientManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelClientManagerLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(addClient)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(editClient)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(deleteClient)
                .addGap(61, 61, 61)
                .addComponent(showAllBorrows)
                .addContainerGap(347, Short.MAX_VALUE))
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 806, Short.MAX_VALUE)
        );
        jPanelClientManagerLayout.setVerticalGroup(
            jPanelClientManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelClientManagerLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelClientManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(editClient)
                    .addComponent(addClient)
                    .addComponent(deleteClient)
                    .addComponent(showAllBorrows))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 459, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jTabbedPaneMain.addTab("Client manager", jPanelClientManager);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPaneMain, javax.swing.GroupLayout.DEFAULT_SIZE, 811, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPaneMain, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonAddMediumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddMediumActionPerformed
         MediumActionDialog dialog = new MediumActionDialog(this, true);
         dialog.setTitle(RBUtils.getBoundle("addMedium"));
         dialog.setVisible(true);
    }//GEN-LAST:event_jButtonAddMediumActionPerformed

    private void jTableMediumsMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableMediumsMouseReleased
    }//GEN-LAST:event_jTableMediumsMouseReleased

    private void jMenuItemAddMediumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemAddMediumActionPerformed
        jButtonAddMediumActionPerformed(evt);
    }//GEN-LAST:event_jMenuItemAddMediumActionPerformed

    private void jButtonEditMediumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditMediumActionPerformed
        if (jTableMediums.getSelectedRowCount() != 0) {
            MediumActionDialog dialog = new MediumActionDialog(this, true);
            Integer row = jTableMediums.getSelectedRow();
            Medium medium = mediumModel.getValueAt(row);
            dialog.setMedium(medium);
            dialog.setTitle(RBUtils.getBoundle("editMedium"));
            dialog.setVisible(true);
        }
    }//GEN-LAST:event_jButtonEditMediumActionPerformed

    private void jMenuItemEditMediumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemEditMediumActionPerformed
         jButtonEditMediumActionPerformed(evt);
    }//GEN-LAST:event_jMenuItemEditMediumActionPerformed

    private void jMenuItemBorrowMediumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemBorrowMediumActionPerformed
        jButtonBorrowMediumActionPerformed(evt);
    }//GEN-LAST:event_jMenuItemBorrowMediumActionPerformed

    private void jButtonBorrowMediumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBorrowMediumActionPerformed
        ClientChooserDialog dialog = new ClientChooserDialog(this, true);
        dialog.setVisible(true);
    }//GEN-LAST:event_jButtonBorrowMediumActionPerformed

    private void addClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addClientActionPerformed
        ClientActionDialog dialog = new ClientActionDialog(this, true);
        dialog.setVisible(true);
}//GEN-LAST:event_addClientActionPerformed

    private void editClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editClientActionPerformed
        ClientActionDialog dialog = new ClientActionDialog(this, true);
        dialog.setVisible(true);
}//GEN-LAST:event_editClientActionPerformed

    private void showAllBorrowsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showAllBorrowsActionPerformed
        ClientChooserDialog dialog = new ClientChooserDialog(this, true);
        dialog.setVisible(true);
}//GEN-LAST:event_showAllBorrowsActionPerformed

    private void jButtonDeleteMediumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteMediumActionPerformed
        int reply = JOptionPane.showConfirmDialog(this, "Skutocne chcete zmazat vybrane medium?", getBoundle("mediumEvidence"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
        if (reply == JOptionPane.YES_OPTION) {
            Integer row = jTableMediums.getSelectedRow();
            Medium medium = mediumModel.getValueAt(row);
            deleteMediumSwingWorker = new DeleteMediumSwingWorker();
            deleteMediumSwingWorker.setMedium(medium);
            deleteMediumSwingWorker.execute();
        }
    }//GEN-LAST:event_jButtonDeleteMediumActionPerformed

    private void jMenuItemDeleteMediumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemDeleteMediumActionPerformed
        jButtonDeleteMediumActionPerformed(evt);
}//GEN-LAST:event_jMenuItemDeleteMediumActionPerformed

    private void jButtonSearchMediumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchMediumActionPerformed
        loadMediumsSwingWorker = new LoadMediumsSwingWorker();
        loadMediumsSwingWorker.setSearchString(jTextFieldSearchMedium.getText().toLowerCase());
        loadMediumsSwingWorker.execute();
    }//GEN-LAST:event_jButtonSearchMediumActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) throws SQLException{
        try {
	    // Set System L&F
            UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
        } 
        catch (UnsupportedLookAndFeelException e) {
            // handle exception
        }
        catch (ClassNotFoundException e) {
            // handle exception
        }
        catch (InstantiationException e) {
            // handle exception
        }
        catch (IllegalAccessException e) {
            // handle exception
        }
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                MainFrame window = new MainFrame();
                window.setVisible(true);
                window.setLocationRelativeTo(null);
                //JOptionPane.showMessageDialog(null," Message",  "Error",  JOptionPane.ERROR_MESSAGE);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addClient;
    private javax.swing.JButton deleteClient;
    private javax.swing.JButton editClient;
    private javax.swing.JButton jButtonAddMedium;
    private javax.swing.JButton jButtonBorrowMedium;
    private javax.swing.JButton jButtonDeleteMedium;
    private javax.swing.JButton jButtonEditMedium;
    private javax.swing.JButton jButtonSearchMedium;
    private javax.swing.JLabel jLabelSearch;
    private javax.swing.JMenuItem jMenuItemAddMedium;
    private javax.swing.JMenuItem jMenuItemBorrowMedium;
    private javax.swing.JMenuItem jMenuItemDeleteMedium;
    private javax.swing.JMenuItem jMenuItemEditMedium;
    private javax.swing.JPanel jPanelClientManager;
    private javax.swing.JPanel jPanelMediumManager;
    private javax.swing.JPopupMenu jPopupMenuMediumList;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPaneMediumsTableCover;
    private javax.swing.JTabbedPane jTabbedPaneMain;
    private javax.swing.JTable jTableClients;
    private javax.swing.JTable jTableMediums;
    private javax.swing.JTextField jTextFieldSearchMedium;
    private javax.swing.JButton showAllBorrows;
    // End of variables declaration//GEN-END:variables

}
